package com.bj58.data.emailreports.sends;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.bj58.data.emailreports.controllers.SEOWeekReportController;
import com.bj58.data.emailreports.dao.SrcDbPool.DBType;
import com.bj58.data.emailreports.model.DataSourceCheck;
import com.bj58.data.emailreports.model.EmailReportUser;
import com.bj58.data.emailreports.utils.DateUtil;

/**SEO周报发送
 * @author huangliang
 *
 */
public class SeoWRSend extends BaseSend{
	
	public static void main(String[] args) {
		SeoWRSend  send = new SeoWRSend();
		SEOWeekReportController  seoReport = new SEOWeekReportController();
		try {
			//参数获取
			String searchDate = DateUtil.getStringDate(new Date(),"yyyy-MM-dd");//查询日期（今）    args -1  查询日期  有效格式 yyyy-MM-dd
			
			if(null != args && args.length > 0){
				if(args.length > 1){
					System.out.println("params error");
					System.exit(0);
				}
				
				if(args[0].contains("-")){
					searchDate =args[0].trim();
				}
			}
			System.out.println(searchDate);
			
			//收件人
			List<EmailReportUser> userListDayReport = send.getUsersBySendType(42);//收件人
			
			
			//数据校验
			String checkStartDate = DateUtil.getStringDate(DateUtil.getDateBefore(DateUtil.strToDate(searchDate, "yyyy-MM-dd"), 14),"yyyy-MM-dd");
			String checkEndDate = DateUtil.getStringDate(DateUtil.getDateBefore(DateUtil.strToDate(searchDate, "yyyy-MM-dd"), 1),"yyyy-MM-dd");
			
			List<DataSourceCheck> checkList = send.getCheckSourceList(checkStartDate, checkEndDate);
			for (DataSourceCheck checkVo : checkList) {
				if(!send.checkDataSource(checkVo)){
					send.errorWarn("SEO数据周报数据源 -"+checkVo.getTableName()+" 校验失败 !");
					System.exit(0);				}
			}
			
			//数据查询
			String beginDate = DateUtil.getStringDate(DateUtil.getDateBefore(DateUtil.strToDate(searchDate, "yyyy-MM-dd"), 7),"yyyy-MM-dd");
			String endDate = DateUtil.getStringDate(DateUtil.getDateBefore(DateUtil.strToDate(searchDate, "yyyy-MM-dd"), 1),"yyyy-MM-dd");
			String contextDayReport =  seoReport.seoWeekReport(endDate);
			if("".equals(contextDayReport)){
				send.errorWarn("SEO数据周报 from "+ beginDate + " to " + endDate );
			}else{
				send.ReportSend(userListDayReport, "SEO数据周报 from "+ beginDate + " to " + endDate  , contextDayReport, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send.errorWarn("SEO数据周报报错!");
		}finally{
			System.exit(0);
		}
	}
	
	
	
	/**获取校验数据源
	 * @param checkStartDate
	 * @param checkEndDate
	 * @return
	 * @throws Exception
	 */
	private List<DataSourceCheck>  getCheckSourceList(String checkStartDate,String checkEndDate)throws Exception{
		
		List<DataSourceCheck> checkList = new ArrayList<DataSourceCheck>();
		
		DataSourceCheck checkVo1 = new DataSourceCheck();
		checkVo1.setTableName("t_netflow_pc_pv_*yyyyMM*");
		checkVo1.setCheckStartDate(checkStartDate);
		checkVo1.setCheckEndDate(checkEndDate);
		checkVo1.setDataColName("stat_date");
		//checkVo1.setDbid(DBType.infobright_26_data_app.getValue());
		checkVo1.setDbid(DBType.db_94_data_app.getValue());
		
		checkList.add(checkVo1);
		
		DataSourceCheck checkVo2 = new DataSourceCheck();
		checkVo2.setTableName("t_netflow_pc_pv_*yyyyMM*");
		checkVo2.setCheckStartDate(checkStartDate);
		checkVo2.setCheckEndDate(checkEndDate);
		checkVo2.setDataColName("stat_date");
		//checkVo2.setDbid(DBType.infobright_26_data_app.getValue());
		checkVo2.setDbid(DBType.db_94_data_app.getValue());
		
		checkList.add(checkVo2);
		
		return checkList;
	}
	
}